package com.ruoyi.crop.service.impl;

import com.alibaba.fastjson.JSON;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.ruoyi.crop.api.domain.AroundTeam;
import com.ruoyi.crop.api.domain.ClassAround;
import com.ruoyi.crop.api.domain.PlanAround;
import com.ruoyi.crop.api.domain.PlanClasses;
import com.ruoyi.crop.mapper.AroundTeamMapper;
import com.ruoyi.crop.mapper.ClassAroundMapper;
import com.ruoyi.crop.mapper.PlanAroundMapper;
import com.ruoyi.crop.mapper.PlanClassesMapper;
import com.ruoyi.crop.service.PlanClassesService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;

/**
* @author 86178
* @description 针对表【src_plan_classes(排班计划表)】的数据库操作Service实现
* @createDate 2024-07-27 09:35:55
*/
@Service
public class PlanClassesServiceImpl extends ServiceImpl<PlanClassesMapper, PlanClasses>
    implements PlanClassesService{
    @Autowired
    ClassAroundMapper classAroundMapper;
    @Autowired
    PlanAroundMapper planAroundMapper;
    @Autowired
    AroundTeamMapper aroundTeamMapper;
    @Override
    public List<PlanClasses> lists(PlanClasses srcPlanClasses) {
        return baseMapper.lists(srcPlanClasses);
    }

    @Override
    @Transactional
    public void deleteBatchByIds(List<Integer> ids) {
        List<PlanClasses> planClasses = baseMapper.selectBatchIds(ids);
        for (PlanClasses planClass : planClasses) {
            Integer planId = planClass.getPlanId();
            System.err.println(planId);
            ClassAround classAround = classAroundMapper.selectOne(Wrappers.lambdaQuery(ClassAround.class).eq(ClassAround::getPlanId, planId));
            System.err.println(JSON.toJSON(classAround));
            if(classAround!=null){
                //中间表
                planAroundMapper.delete(Wrappers.lambdaQuery(PlanAround.class).eq(PlanAround::getAroundId, classAround.getId()));
                aroundTeamMapper.delete(Wrappers.lambdaQuery(AroundTeam.class).eq(AroundTeam::getAroundId, classAround.getId()));
                //班组表
                classAroundMapper.delete(Wrappers.lambdaQuery(ClassAround.class).eq(ClassAround::getPlanId, planId));
            }
        }
       baseMapper.deleteBatchIds(ids);
    }
}




